package com.example.demo.config;

import com.example.demo.interceptor.JwtInterceptor;
import com.example.demo.utils.CustomDateFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.beans.factory.annotation.Value;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Autowired
    private CustomDateFormatter customDateFormatter;

    @Value("${file.upload.dir}")
    private String fileUploadDir;

    @SuppressWarnings("null")
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(customDateFormatter);
    }

    @SuppressWarnings("null")
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/admin/**", "/**") // 优先拦截/admin/开头的请求
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/notice/*")
                .excludePathPatterns("/uploads/*")
                ; // 排除登录接口
    }

    @SuppressWarnings("null")
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源访问
        registry.addResourceHandler("/uploads/**")
                // .addResourceLocations("file:///d:/CODE/DEEPSEEK/myproject/uploads/")
                .addResourceLocations("file:" + fileUploadDir);
    }
}
